﻿using MediatR;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week03.wenk
{
    //生产划膘单列表
    public class ScratchesScrentenHandler : IRequestHandler<ScratchesScrentenCommend, Result>
    {
        private readonly IBaseRepositoryGet<Scratches> Scratches;
        private readonly IBaseRepositoryGet<Production> Production;
        private readonly IBaseRepositoryGet<Make> Make;

        public ScratchesScrentenHandler(IBaseRepositoryGet<Scratches> Scratches, IBaseRepositoryGet<Production> Production,
            IBaseRepositoryGet<Make> Make)
        {
            this.Scratches = Scratches;
            this.Production = Production;
            this.Make = Make;
        }

        public async Task<Result> Handle(ScratchesScrentenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };

            try
            {
                var list = (from a in Scratches.GetAll()
                            join b in Production.GetAll() on a.ProductionId equals b.ProductionId
                            join c in Make.GetAll() on a.MakeId equals c.MakeId
                            where a.IsDeleted == false
                            select new
                            {
                                a.ScratchesId,
                                a.ScratchesDate,
                                a.ScratchesDocument,
                                a.ScratchesSummary,
                                a.ScratchesSlaughter,
                                a.ScratchesState,
                                a.ScratchesReviewers,
                                a.ScratchesTime,
                                a.MakeTime,
                                a.MakeUpdateTime,
                                b.ProductionId,
                                b.ProductionName,
                                c.MakeId,
                                c.MakeName,
                            }).ToList();

                

                if (!string.IsNullOrEmpty(request.ScratchesDate))
                {
                    list = list.Where(x =>x.ScratchesDate == Convert.ToDateTime( request.ScratchesDate)).ToList();
                }

                if (!string.IsNullOrEmpty(request.ScratchesDocument))
                {
                    list = list.Where(x => x.ScratchesDocument.Contains(request.ScratchesDocument)).ToList();
                }

                if (!string.IsNullOrEmpty(request.ScratchesSlaughter))
                {
                    list = list.Where(x => x.ScratchesSlaughter.Contains(request.ScratchesSlaughter)).ToList();
                }

                if (request.ProductionId >0)
                {
                    list=list.Where(x=>x.ProductionId==request.ProductionId).ToList();
                }

                var totalcount = list.Count();
                var pagecount = (int)Math.Ceiling((totalcount * 1.0) / request.pageSize);
                list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

                var lief = new
                {
                    totalcount,
                    pagecount,
                    list,
                };

                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = lief;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
